<?php
/**
 * Created by PhpStorm.
 * User: this
 * Date: 2018/12/8
 * Time: 15:40
 */
namespace app\admin\model;
use think\Model;
class Products extends Model{
    /**
     * @param $condition
     * @param string $field
     * @param $page
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getPage($condition,$field = '*', $page = 10){
        return $this->name('product')->field($field)->join('bb_product_category','bb_product_category.cate_id=bb_product.product_cate_id')->where($condition)->order('bb_product.product_id asc')->cache(true,5)->paginate($page);
    }

    /**
     * @param $condition
     * @param string $field
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($condition,$field = '*'){
        return $this->name('product')->field($field)->where($condition)->cache(true)->find();
    }

    /**
     * @param $data
     * @return int|string
     */
    public function add($data){
        return $this->name('product')->insert($data);
    }

    /**
     * @param $condition
     * @return int
     */
    public function del($condition){
        return $this->name('product')->where($condition)->delete();
    }

    /**
     * @param $condition
     * @param $data
     * @return $this
     */
    public function edit($condition,$data){
        return $this->name('product')->where($condition)->update($data);
    }

    /**
     * @param $condition
     * @param string $field
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($condition,$field = '*'){
        return $this->name('product')->field($field)->where($condition)->cache(true,10)->select();
    }


    /**
     * @param $condition
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cateOne($condition,$field){
        return $this->name('product_category')->field($field)->where($condition)->find();
    }

    /**
     * @param $condition
     * @param $field
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCate($condition,$field='*'){
        return $this->name('product_category')->field($field)->where($condition)->order('cate_pid asc')->cache(true)->select();
    }

    /**
     * @param $condition
     * @param string $field
     * @param $page
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function cagePage($condition,$field = '*',$page = 10){
        return $this->name('product_category')->field($field)->where($condition)->order('cate_id asc')->cache(true)->paginate($page);
    }
    /**
     * @param $data
     * @return int|string
     */
    public function cateAdd($data){
        return $this->name('product_category')->insert($data);
    }

    /**
     * @param $condition
     * @return int
     */
    public function cateDel($condition){
        return $this->name('product_category')->where($condition)->delete();
    }

    public function cateEdit($condition,$data){
        return $this->name('product_category')->where($condition)->update($data);
    }
}